/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import database_beans.Korisnik;
import database_beans.Role;
import database_beans.Student;
import database_beans.Zaposleni;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import server.Log;
import server.UserLoginAndLogout;
import server.Util;

/**
 *
 * @author stefan
 */
@ManagedBean
@SessionScoped
public class LoginBean implements Serializable{

    private String username;
    private String password;

    /**
     * Creates a new instance of LoginBean
     */
    public LoginBean() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String attempLogin() {
        
        String retPage = "";
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession s = (HttpSession) facesContext.getExternalContext().getSession(false);
        ////////////////////////////////////////
        Korisnik user = UserLoginAndLogout.getInstance().login(username, password);
        if (user != null) {
            Session session = Util.getSesion();
            Transaction t = session.beginTransaction();
            Role rola = user.getRole();
            Log.i("ID role je " + rola.getIdRole());
            if (rola.getIdRole() == 1) {// ADMIN
                Criteria cr = session.createCriteria(Zaposleni.class);
                cr.add(Restrictions.eq("idKorisnik", user.getIdKorisnik()));
                Log.i("ID korsnika je " + user.getIdKorisnik());
                if (!cr.list().isEmpty()) {
                    Zaposleni zaposleni = (Zaposleni) cr.list().get(0);
                    s.setAttribute(Util.SESSION_USER, zaposleni);
                    s.setAttribute(Util.SESSION_USER_ID, 1);
                    retPage = "admin?faces-redirect=true";
                }
            } else if (rola.getIdRole() == 2) {
                Criteria cr = session.createCriteria(Zaposleni.class);
                cr.add(Restrictions.eq("idKorisnik", user.getIdKorisnik()));
                Log.i("ID korsnika je " + user.getIdKorisnik());
                if (!cr.list().isEmpty()) {
                    Zaposleni zaposleni = (Zaposleni) cr.list().get(0);
                    s.setAttribute(Util.SESSION_USER, zaposleni);
                    s.setAttribute(Util.SESSION_USER_ID, 2);
                    retPage = "profilZaposleni?faces-redirect=true";
                }
            } else {
                Criteria cr = session.createCriteria(Student.class);
                cr.add(Restrictions.eq("idKorisnik", user.getIdKorisnik()));
                Log.i("ID korsnika je " + user.getIdKorisnik());
                if (!cr.list().isEmpty()) {
                    Student student = (Student) cr.list().get(0);                    
                    s.setAttribute(Util.SESSION_USER, student);
                    s.setAttribute(Util.SESSION_USER_ID, 3);
                    retPage = "student?faces-redirect=true";
                }
            }
            t.commit();
        }else{
            facesContext.addMessage(null, new FacesMessage("Uneli ste neispravne login podatke!!!"));
        }
        
        return retPage;
    }
}
